#!/data/python-env/bin/python3
# -*- coding: utf-8 -*-

import json

import openpyxl


def convert():
    with open('./a.json', 'r') as f:
        data = json.loads(f.read())
    write(data)


def write(data):
    path = './first.xlsx'
    wb = openpyxl.load_workbook(path)
    sheet = wb["Sheet1"]
    max_row = 0
    last_idx = 0
    for val in sheet.values:
        if len(val) <= 0:
            break
        last_idx = val[0]
        max_row += 1
    print('原来表总长度', sheet.max_row, "最后一行id：", last_idx)

    i = 0
    for key in data:
        i += 1
        line = data[key]
        line_id = str(i + max_row)
        sheet['A' + line_id] = last_idx + i
        sheet['B' + line_id] = ",".join(line['word'])
        sheet['F' + line_id] = ",".join(line['idiom'])

        posx_list = []
        for v in line['posx']:
            posx_list.append(str(v))
        sheet['C' + line_id] = ",".join(posx_list)

        posy_list = []
        for v in line['posy']:
            posy_list.append(str(v))
        sheet['D' + line_id] = ",".join(posy_list)

        answer_list = []
        for v in line['answer']:
            answer_list.append(str(v))
        sheet['E' + line_id] = ",".join(answer_list)

        barrier_list = []
        for v in line['barrier']:
            barrier_list.append(str(v))
        sheet['G' + line_id] = ",".join(barrier_list)

        if i % 100 == 0:
            print("构造", i, "关成功")

    wb.save('./first.xlsx')
    print("保存成功")


convert()

